package com.Action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.FileOperation;
import transformFactory.TransformDate;

import com.Entity.Media;
import com.Entity.Mediacomment;
import com.Service.MediaCommentService;
import com.Service.MediaService;
import com.opensymphony.xwork2.ActionContext;

public class MediaAction {
	
	private static final Logger logger = Logger.getLogger(MediaAction.class);
	
	private static final long serialVersionUID = 572146812454l;
	private static final String SUCCESS1 = "success1";
	private static final String SUCCESS2 = "success2";

	private Media myMedia=new Media();
	@Autowired
	private MediaService mediaservice;
	private List<Media> medialist;
	private List<Media> hosmedialist;

	private Mediacomment mediacomment=new Mediacomment();
	@Autowired
	private MediaCommentService mediacommentservice;
	private List<Mediacomment> mediacommentlist;
	
	private Map request = null;
	
	private Media media=new Media();
	private File myFile;
	private String contentType;
	private String fileName;
	private String imageFileName;
	private String caption;
	String title=null;//视频名称
	String src=null;//视频的位置
	String picture=null;//可能用于存储视频照片
	String descript=null;//视频描述
	String uptime=null;//视频时间
	String type=null;//视频类
	
	String ffmpegPath;
	String upFilePath;
	String codcFilePath;
	String mediaPicPath;
	
	
	HttpSession session = ServletActionContext.getRequest().getSession();
	
	public String uploadVideos() throws Exception{
		
		int userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		int usertype=Integer.parseInt(ServletActionContext.getRequest().getParameter("usertype"));
		title=FileOperation.getFileNameNoEx(ServletActionContext.getRequest().getParameter("fileName"));
		String realname=ServletActionContext.getRequest().getParameter("realName");
		src="http://211.83.105.71:8888/MonitorSystem_V1.0/UploadVideos/"+realname;
		ffmpegPath="d:\\ffmpeg\\bin\\ffmpeg.exe";
		upFilePath=ServletActionContext.getServletContext().getRealPath("/UploadVideos")+"\\"+realname;
		logger.info("upFilePath:"+upFilePath);
        String picfilename=FileOperation.getFileNameNoEx(realname)+".jpg";
        mediaPicPath=ServletActionContext.getServletContext().getRealPath("/UploadVideos")+"\\"+picfilename;
        logger.info("mediaPicPath:"+mediaPicPath);
        picture="http://211.83.105.71:8888/MonitorSystem_V1.0/UploadVideos/"+picfilename;
        logger.info("picture:"+picture);
		
        this.mediaservice.screenShot(ffmpegPath, upFilePath, mediaPicPath);
		
		logger.info("userid:"+userid);
		logger.info("usertype:"+usertype);
		logger.info("title:"+title);
		logger.info("src:"+src);
		myMedia.setTitle(title);
		myMedia.setSrc(src);
		myMedia.setPicture(picture);
		myMedia.setUptime(new Date());
		myMedia.setUserId(userid);
		myMedia.setUserType((short) usertype);
		logger.info("set complete!");
		this.mediaservice.save(myMedia);
		return "uploadVideosSucc";
	}
	
	
	public String listVideos() throws Exception {
		int userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		medialist=this.mediaservice.findVideosByUserId(userid);
		hosmedialist=this.mediaservice.findHosVideos();
		return "listVideosSucc";
	}
	
	
	public String playVideo() throws Exception {
		int userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		int videoid=Integer.parseInt(ServletActionContext.getRequest().getParameter("videoid"));
		logger.info("videoid:"+videoid);
		myMedia=this.mediaservice.getById(videoid);
		medialist=this.mediaservice.findVideosByUserId(userid);
		session.setAttribute("myMediaSrc", myMedia.getSrc());
		session.setAttribute("myMediaPic", myMedia.getPicture());
		session.setAttribute("videoId", videoid);
		mediacommentlist=this.mediacommentservice.findCommentByVideoId((long)videoid);
		logger.info("commenttext"+mediacommentlist.get(0).getCommentText());
		return "playVideoSucc";
	}

	public String addMediaComment() throws Exception {
		String authorname=ServletActionContext.getRequest().getParameter("authorName");
		long commentNum=this.mediacommentservice.countTotalCommentByVideoId(mediacomment.getVideoId());
		mediacomment.setAuthorName(authorname);
		mediacomment.setCommentTime(new Date());
		mediacomment.setFloorNum((int)commentNum+1);
		mediacomment.setIsReplyComment(0);
		this.mediacommentservice.save(mediacomment);
		mediacommentlist=this.mediacommentservice.findCommentByVideoId((int) mediacomment.getVideoId());
		return "addMediaCommentSucc";
	}
	
	public String writeMediaComment() throws Exception {
		logger.info("begin reply");
		String originalcommentorname=ServletActionContext.getRequest().getParameter("originalCommentorName");
		originalcommentorname =new String(originalcommentorname.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(originalcommentorname);
		String originalreplytime=ServletActionContext.getRequest().getParameter("originalReplyTime");
		session.setAttribute("originalReplyTime", originalreplytime);
		logger.info(originalreplytime);
		long originalcommentid=Integer.parseInt(ServletActionContext.getRequest().getParameter("originalCommentId"));
		logger.info(originalcommentid);
		String originalcommenttext=ServletActionContext.getRequest().getParameter("originalCommentText");
		originalcommenttext =new String(originalcommenttext.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(originalcommenttext);
		mediacomment.setOriginalCommentorName(originalcommentorname);
		mediacomment.setOriginalCommentId(originalcommentid);
		mediacomment.setOriginalCommentText(originalcommenttext);
		return "writeMediaCommentSucc";
	}
	
	public String replyMediaComment() throws Exception {
			String strReplytime=(String) session.getAttribute("originalReplyTime");
			Date originalreplytime=TransformDate.Str2Date(strReplytime);
			long commentNum=this.mediacommentservice.countTotalCommentByVideoId(mediacomment.getVideoId());
			mediacomment.setCommentTime(new Date());
			mediacomment.setFloorNum((int)commentNum+1);
			mediacomment.setIsReplyComment(1);
			mediacomment.setOriginalReplyTime(originalreplytime);
			this.mediacommentservice.save(mediacomment);
			mediacommentlist=this.mediacommentservice.findCommentByVideoId((int) mediacomment.getVideoId());
		return "replyMediaCommentSucc";
	}
	

	
	public String transcodingVideo() throws Exception {
		
		try {
			String fileName="\\He's a Pirate.mp4";
			/*String fileName="\\Drunk On You.mp4";*/
			String imgName="\\321.jpg";
			logger.info("ok!");
			/*ffmpegPath=ServletActionContext.getServletContext().getRealPath("\\tools\\ffmpeg.exe");*/
			ffmpegPath="d:\\ffmpeg\\bin\\ffmpeg.exe";
			upFilePath="d:\\He's a Pirate.flv";
			/*upFilePath="d:\\Drunk On You.flv";*/
			logger.info("upFilePath:"+upFilePath);
			
			String uploadFilePath=ServletActionContext.getServletContext().getRealPath("\\UploadVideos");
			logger.info("uploadFilePath:"+uploadFilePath);
			File dirFile = new File(uploadFilePath);
			// 判断当前文件夹（路径）是否存在，如果不存在则创建
	        if (!dirFile.exists()) {
	        	dirFile.mkdirs();
	        }
	        //为文件重命名
/*			fileName = java.util.UUID.randomUUID().toString();
			fileName += uploadFileName.substring(uploadFileName.length() - 4);  
			File toFile = new File(uploadPath, fileName);*/
/*	        codcFilePath="d:\\He's a Pirate.mp4";
	        mediaPicPath="d:\\He's a Pirate.jpg";*/
			codcFilePath=uploadFilePath+fileName;
			mediaPicPath=uploadFilePath+imgName;
			this.mediaservice.executeCodecs(ffmpegPath,upFilePath,codcFilePath,mediaPicPath);
			/*this.mediaservice.screenShot(ffmpegPath, upFilePath, mediaPicPath);*/
			logger.info("mediaPicPath:"+mediaPicPath);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "transcodingVideoSucc";
	}
	
	
	

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getDescript() {
		return descript;
	}

	public void setDescript(String descript) {
		this.descript = descript;
	}

	public String getUptime() {
		return uptime;
	}

	public void setUptime(String uptime) {
		this.uptime = uptime;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Media getMedia() {
		return media;
	}

	public void setMedia(Media media) {
		this.media = media;
	}
	
	public void setMyFileContentType(String contentType) {
		this.setContentType(contentType);
	}

	public void setMyFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}
	
	public List<Media> getMedialist() {
		return medialist;
	}

	public void setMedialist(List<Media> medialist) {
		this.medialist = medialist;
	}

    public Media getMyMedia() {
		return myMedia;
	}

	public void setMyMedia(Media myMedia) {
		this.myMedia = myMedia;
	}

	public List<Media> getHosmedialist() {
		return hosmedialist;
	}

	public void setHosmedialist(List<Media> hosmedialist) {
		this.hosmedialist = hosmedialist;
	}
	public Mediacomment getMediacomment() {
		return mediacomment;
	}


	public void setMediacomment(Mediacomment mediacomment) {
		this.mediacomment = mediacomment;
	}


	public List<Mediacomment> getMediacommentlist() {
		return mediacommentlist;
	}


	public void setMediacommentlist(List<Mediacomment> mediacommentlist) {
		this.mediacommentlist = mediacommentlist;
	}
}
